'use strict';
	angular.module('accounting.accountYearSingle', [])

	.controller('accountYearSingleCtrl'
			,['$rootScope', '$scope', 'BizData'
			   , f])//控制器结束
	function f($rootScope,$scope, BizData
					   ) {
		var Model = this;
		
		//定义页面级别的变量

		var billType = 'KJQJ';
		var billTypeName = "会计期间";
		var systemCode = $rootScope.winid; //系统代码

		/////////////////////////////////////////////////////////////////////////////////////////////////
		
		var userId;
		var enterpriseNo;
		var enterpriseName;
		
		var yearNo;
		var orderDate;
		
		
		/**
		name:bizData#onNewCreateParam
		description: <b>[回调型事件]</b>业务新增数据创建新增参数事件，可以增加和修改用户自定义的参数
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"param" : {justep.Request.ActionParam} 新增参数对象,
			"defaultValues" : array - 新增行的默认值数组 ，数组的长度决定新增行数，数组中的对象是列的默认值
				defaultValues格式：
					[
						{列名:默认值,...},
						{列名:默认值,...},
						...
					]
		}
		</xmp>	
		@see justep.XData#newData
		@see justep.Request.ActionParam
		*/
		Model.dataMasterNewCreateParam = function(event) {
			event.param = {"billType": billType};
		};

		/**
		name:bizData#onBeforeNew
		description: <b>[回调型事件]</b>业务数据新增前
		@param {object} event 它的结构如下:<br/>{"source":组件的js对象,"cancel":可修改，设置为true后中断当前new动作}
		 */
		Model.dataMasterBeforeNew = function(event) {
			
		};
		/**
		 * name:bizData#onAfterNew description: <b>[回调型事件]</b>业务数据新增后
		 * 
		 * @param event
		 *            它的结构如下:<br/>{"source":组件的js对象,"id":新增的行Id}
		 */
		Model.dataMasterAfterNew = function(event){
			
			event.source.setValue('billType',billType,event.id);
			event.source.setValue('billTypeName',billTypeName,event.id);
			event.source.setValue('systemCode',systemCode,event.id);

		};

		/**
		name:bizData#onBeforeDelete
		description: <b>[回调型事件]</b>业务数据删除前
		@param {object} event 它的结构如下:<br/>
		{
			"source":组件的js对象
			,"cancel":可修改	，设置为true后中断当前delete动作
			,"deleteIDList":删除的行Id数组
		}
		*/
		Model.dataMasterBeforeDelete = function(event) {
//			alert(event.deleteIDList[0]);
		};
		
		/**
		name:bizData#onAfterDelete
		description: <b>[回调型事件]</b>业务数据删除后
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"deleteIDList" : 删除的行Id数组
		}
		</xmp>	
		*/
		Model.dataMasterAfterDelete = function(event) {
//			alert('删除之后！')
		};

		
		
		/**
		name:bizData#onIndexChanged
		description: <b>[回调型事件]</b>行记录变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"rowID" : 行Id,
			"rowIndex" : 行索引
		}
		</xmp>	
		*/
		Model.dataMasterIndexChanged = function(event) {
			if(event.rowID!=null){
				Model.dataDetail.fMasterID = event.rowID;
	       		Model.dataDetail.refreshData();
			}else{
				Model.dataDetail.clearData();
			}
		};
		
		/**
		name:bizData#onDataChanged
		description: <b>[回调型事件]</b>数据发生变化（包括新增、刷新、删除、修改引起的变化），感知从data的变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"changedSource" ：触发变化的组件的js对象，当从data触发时返回从data对象,
			"selfChanged" : 是否自身变化，false：从data触发的变化,
			"type" : 触发变化的类型，包括：new、delete、refresh、valueChanged、clear，其他参数根据触发类型不同给出不同参数，参考onAfterNew等事件参数
		}
		</xmp>	
		*/
		Model.dataMasterDataChanged = function(event) {
			
		};
		
		/**
		name:bizData#onValueChanged
		description: <b>[回调型事件]</b>数据变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"column" : 关系,
			"rowIndex" : 行索引,
			"value" : 新值,
			"originalValue" : 旧值
		}
		</xmp>	
		*/
		//输入值变化，监视input的输入，ng-change
		Model.dataMasterValueChanged = function(event) {
			
		};
		
		
		Model.dataMasterBeforeSave = function(event) {
			console.log('dataMasterBeforeSave-->',event);
		};
		
		
		Model.dataMasterAfterSave = function(event) {
//			console.log('dataMasterAfterSave.event-->',event);
			//这是为了后台生成的客户编码能够刷新到前台来！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！
			Model.dataMaster.refreshPageData();			
		};
		
		

		/**
		name:bizData#onBeforeDelete
		description: <b>[回调型事件]</b>业务数据删除前
		@param {object} event 它的结构如下:<br/>
		{
			"source":组件的js对象
			,"cancel":可修改	，设置为true后中断当前delete动作
			,"deleteIDList":删除的行Id数组
		}
		*/
		Model.dataDetailBeforeDelete = function(event) {
//			alert(event.deleteIDList[0]);
//			event.cancel = true;
		};
		/**
		name:bizData#onAfterDelete
		description: <b>[回调型事件]</b>业务数据删除后
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"deleteIDList" : 删除的行Id数组
		}
		</xmp>	
		*/
		Model.dataDetailAfterDelete = function(event) {
//			alert('删除之后！')
		};
		
		
		
		/**
		name:bizData#onIndexChanged
		description: <b>[回调型事件]</b>行记录变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"rowID" : 行Id,
			"rowIndex" : 行索引
		}
		</xmp>	
		*/
		Model.dataDetailIndexChanged = function(event) {

		};
		
		/**
		name:bizData#onDataChanged
		description: <b>[回调型事件]</b>数据发生变化（包括新增、刷新、删除、修改引起的变化），感知从data的变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"changedSource" ：触发变化的组件的js对象，当从data触发时返回从data对象,
			"selfChanged" : 是否自身变化，false：从data触发的变化,
			"type" : 触发变化的类型，包括：new、delete、refresh、valueChanged、clear，其他参数根据触发类型不同给出不同参数，参考onAfterNew等事件参数
		}
		</xmp>	
		*/
		Model.dataDetailDataChanged = function(event) {
			
		};
		
		/**
		name:bizData#onValueChanged
		description: <b>[回调型事件]</b>数据变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"column" : 关系,
			"rowIndex" : 行索引,
			"value" : 新值,
			"originalValue" : 旧值
		}
		</xmp>	
		*/
		//输入值变化，监视input的输入，ng-change
		Model.dataDetailValueChanged = function(event) {
			
		};
		

		/**
		name:bizData#onBeforeNew
		description: <b>[回调型事件]</b>业务数据新增前
		@param {object} event 它的结构如下:<br/>{"source":组件的js对象,"cancel":可修改，设置为true后中断当前new动作}
		 */
		Model.dataDetailBeforeNew = function(event) {
			// 明细数据新增之前
			// 检查主表中的数据是否完整
			var dataMaster = Model.dataMaster;
			var rowID = dataMaster.getCurrentID();
			
			userId = dataMaster.getValue('userId', rowID);
			enterpriseNo = dataMaster.getValue('enterpriseNo', rowID);
			enterpriseName = dataMaster.getValue('enterpriseName', rowID);
			
			yearNo = dataMaster.getValue('yearNo', rowID);
			orderDate = dataMaster.getValue('orderDate', rowID);
			

		};
		/**
		 * name:bizData#onAfterNew description: <b>[回调型事件]</b>业务数据新增后
		 * 
		 * @param event
		 *            它的结构如下:<br/>{"source":组件的js对象,"id":新增的行Id}
		 */
		Model.dataDetailAfterNew = function(event){
			var dataDetail = event.source;
			var rowID = event.id;

			dataDetail.setValue('userId', userId, rowID);
			dataDetail.setValue('enterpriseNo', enterpriseNo, rowID);
			dataDetail.setValue('enterpriseName', enterpriseName, rowID);
			
			dataDetail.setValue('yearNo',yearNo,rowID);
			dataDetail.setValue('orderDate',orderDate,rowID);

			dataDetail.setValue('fMasterID',dataDetail.fMasterID,rowID);

		};
		
		///////////////////按钮组开始///////////////////////////////////////////////////
		//新建
		Model.add = function(){
			
		     var options = {
			         index : 0,
			         defaultValues : [
			             {
			            	 state:'active'
			            	 ,status:'open'
			            	 ,audit:'active'
			            	 
			            	 ,billType: billType
			            	 ,orderDate: ':getCurrentDate()'
				             ,employeeNo: ':getCurrentPsmFID()'
					         ,employeeName: ':getCurrentPsmFName()'
				             ,cUsercode: ':getCurrentPersonCode()'
					         ,cUsername: ':getCurrentPersonName()'
						     ,cDatetime: ':getCurrentDateTime()'
							 ,userId: ':getCurrentPersonID()'
							 ,enterpriseNo: ':getCurrentOgnFID()'
							 ,enterpriseName: ':getCurrentOgnLongName()'
							 ,version: 0

			            }//,
//			             {itemNo:'a2',itemDescribe:'b2'}
			         ]
			     };    

			Model.dataMaster.newData(options);

			if($scope.tabpanel!='single'){
				$scope.tabpanel='single'
			}
		};
		
		//修改
		Model.edit = function(){
			Model.dataMaster.editData();
			if($scope.tabpanel!='single'){
				$scope.tabpanel='single'
			}
		};
		
		//保存
		Model.save = function() {
			Model.dataMaster.saveData();
		};

		//取消保存
		Model.saveCancel = function() {
			Model.dataMaster.editCancel();
		};
		
		/*
		//不提示直接提交后台删除当前行
		var data = justp.xbl('mainData');
		var deleteConfirm = data.deleteConfirm;
		var directDeleteMode = data.directDeleteMode;
		try{
		    data.deleteConfirm = false;
		    data.directDeleteMode = true;
		    data.deleteData(data.getID());
		}finally{
		    data.deleteConfirm = deleteConfirm;
		    data.directDeleteMode = directDeleteMode;
		}
		*/
		//删除
		Model.remove = function() {
			/*
			/////////////////////////////////////////////////////////////
			var dataMaster = Model.dataMaster;
			var rowID = dataMaster.getCurrentID();
			var userId = dataMaster.getValue('userId',rowID);
			if(userId!=$rootScope.getCurrentPersonID()){
				systemService.dialogMsg('当前数据不是由您创建，您无权进行此操作！');
				return;
			}
			////////////////////////////////////////////////////////////////
			*/
			Model.dataMaster.deleteData(Model.dataMaster.getCurrentID());
			Model.dataMaster.refreshData();
		};
		
		//刷新
		Model.refresh = function() {
			Model.dataMaster.refreshData();
		};
		///////////////////按钮组结束///////////////////////////////////////////////////
		///////////////////子表按钮组开始///////////////////////////////////////////////////
		//增加
		Model.addrow = function(){
			/*
			var dataMaster = Model.dataMaster;
			var rowID = dataMaster.getCurrentID();
			var yearNo = dataMaster.getValue("yearNo", rowID);
			var dateStart = dataMaster.getValue("dateStart", rowID);

			//1、添加记录
			//1月份
			var periodNo1 = yearNo + "-01";
			var dateStart1 = periodNo1 + "-01";
			var dateEnd1 = periodNo1 + "-31";
			console.log('periodNo1-->>',periodNo1);
			console.log('dateStart1-->>',dateStart1);
			console.log('dateEnd1-->>',dateEnd1);
			//2月份
//			var curentYear=justep.String.toInt(yearNo, 0);
			var leapYear=false ;
//			false为平年，true为闰年(1、能被4整除，但是不能被100整除。2、能被400整除。)
			
			var year = parseInt(yearNo);//2016;
		    if (year % 4 == 0 && year % 100 !== 0 || year % 400 == 0) {
		        console.log("今年是闰年！");
		        leapYear = true;
		    } else {
		        console.log("今年是平年！");
		        leapYear = false;
		    }
		    
//			leapYear=justep.Date.isLeapYear(justep.Date.fromString(dateStart,justep.Date.STANDART_FORMAT_SHOT));
			if (leapYear == true){
				var periodNo2 = yearNo + "-02";
				var dateStart2 = periodNo2 + "-01";
				var dateEnd2 = periodNo2 + "-29";
			}else{
				var periodNo2 = yearNo + "-02";
				var dateStart2 = periodNo2 + "-01";
				var dateEnd2 = periodNo2 + "-28";
			}
			//3月份
			var periodNo3 = yearNo + "-03";
			var dateStart3 = periodNo3 + "-01";
			var dateEnd3 = periodNo3 + "-31";
			//4月份
			var periodNo4 = yearNo + "-04";
			var dateStart4 = periodNo4 + "-01";
			var dateEnd4 = periodNo4 + "-30";
			//5月份
			var periodNo5 = yearNo + "-05";
			var dateStart5 = periodNo5 + "-01";
			var dateEnd5 = periodNo5 + "-31";
			//6月份
			var periodNo6 = yearNo + "-06";
			var dateStart6 = periodNo6 + "-01";
			var dateEnd6 = periodNo6 + "-30";
			//7月份
			var periodNo7 = yearNo + "-07";
			var dateStart7 = periodNo7 + "-01";
			var dateEnd7 = periodNo7 + "-31";
			//8月份
			var periodNo8 = yearNo + "-08";
			var dateStart8 = periodNo8 + "-01";
			var dateEnd8 = periodNo8 + "-31";
			//9月份
			var periodNo9 = yearNo + "-09";
			var dateStart9 = periodNo9 + "-01";
			var dateEnd9 = periodNo9 + "-30";
			//10月份
			var periodNo10 = yearNo + "-10";
			var dateStart10 = periodNo10 + "-01";
			var dateEnd10 = periodNo10 + "-31";
			//11月份
			var periodNo11 = yearNo + "-11";
			var dateStart11 = periodNo11 + "-01";
			var dateEnd11 = periodNo11 + "-30";
			//12月份
			var periodNo12 = yearNo + "-12";
			var dateStart12 = periodNo12 + "-01";
			var dateEnd12 = periodNo12 + "-31";

			var defaultValues = [
			{periodNo:periodNo1,yearNo:yearNo,dateStart:dateStart1,dateEnd:dateEnd1,state:'active',status:'open',audit:'active',systemCode:systemCode,billType:billType,billTypeName:billTypeName},
			{periodNo:periodNo2,yearNo:yearNo,dateStart:dateStart2,dateEnd:dateEnd2,state:'active',status:'open',audit:'active',systemCode:systemCode,billType:billType,billTypeName:billTypeName},
			{periodNo:periodNo3,yearNo:yearNo,dateStart:dateStart3,dateEnd:dateEnd3,state:'active',status:'open',audit:'active',systemCode:systemCode,billType:billType,billTypeName:billTypeName},
			{periodNo:periodNo4,yearNo:yearNo,dateStart:dateStart4,dateEnd:dateEnd4,state:'active',status:'open',audit:'active',systemCode:systemCode,billType:billType,billTypeName:billTypeName},
			{periodNo:periodNo5,yearNo:yearNo,dateStart:dateStart5,dateEnd:dateEnd5,state:'active',status:'open',audit:'active',systemCode:systemCode,billType:billType,billTypeName:billTypeName},
			{periodNo:periodNo6,yearNo:yearNo,dateStart:dateStart6,dateEnd:dateEnd6,state:'active',status:'open',audit:'active',systemCode:systemCode,billType:billType,billTypeName:billTypeName},
			{periodNo:periodNo7,yearNo:yearNo,dateStart:dateStart7,dateEnd:dateEnd7,state:'active',status:'open',audit:'active',systemCode:systemCode,billType:billType,billTypeName:billTypeName},
			{periodNo:periodNo8,yearNo:yearNo,dateStart:dateStart8,dateEnd:dateEnd8,state:'active',status:'open',audit:'active',systemCode:systemCode,billType:billType,billTypeName:billTypeName},
			{periodNo:periodNo9,yearNo:yearNo,dateStart:dateStart9,dateEnd:dateEnd9,state:'active',status:'open',audit:'active',systemCode:systemCode,billType:billType,billTypeName:billTypeName},
			{periodNo:periodNo10,yearNo:yearNo,dateStart:dateStart10,dateEnd:dateEnd10,state:'active',status:'open',audit:'active',systemCode:systemCode,billType:billType,billTypeName:billTypeName},
			{periodNo:periodNo11,yearNo:yearNo,dateStart:dateStart11,dateEnd:dateEnd11,state:'active',status:'open',audit:'active',systemCode:systemCode,billType:billType,billTypeName:billTypeName},
			{periodNo:periodNo12,yearNo:yearNo,dateStart:dateStart12,dateEnd:dateEnd12,state:'active',status:'open',audit:'active',systemCode:systemCode,billType:billType,billTypeName:billTypeName}
			];
			*/

			var options = {
			         index : Model.dataDetail.getCount(),
			         defaultValues : [{state:'active',status:'open',audit:'active',systemCode:systemCode,billType:billType,billTypeName:billTypeName}]
			     };    

			Model.dataDetail.newData(options);
		};
		
		Model.delrow = function() {
			 if(Model.dataDetail.directDeleteMode){
				//直接删除
				 if(Model.dataDetail.deleteData(Model.dataDetail.getCurrentID())==0){
//					 Model.dataDetail.refreshData();
				 }
			 }else{
				//前台移除
				 if(Model.dataDetail.removeData(Model.dataDetail.getCurrentID())==0){
//					 Model.dataDetail.recordState = 'delete';
				 }
			 }
		};
		///////////////////子表按钮组结束///////////////////////////////////////////////////

		

		
		//双击事件
		Model.gridListRowDblclick = function(rowIndex,rowID){
			$scope.tabpanel = 'single';
		}
		

		/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//		Model.objects = {"dataMaster" : Model.dataMaster,"dataDetail" : Model.dataDetail};
		
		var options = {};
		options.container = Model;
		options.concept = 'AccountYear';
		options.name = 'dataMaster';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
		options.creater = '/accounting/accountYear/single/create';
		options.reader = '/accounting/accountYear/single/query';
		options.writer = '/accounting/accountYear/single/save';
		options.deleter = '/accounting/accountYear/single/delete';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
		options.isTree = false;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.pathRelation = '';
		options.master = {"data":"","relation":""};
		options.directDeleteMode = true;
		options.limit = 15;
		options.orderBy = 'yearNo DESC';

		options.onIndexChanging = null;
		options.onIndexChanged = Model.dataMasterIndexChanged;
		options.onBeforeNew = Model.dataMasterBeforeNew;
		options.onAfterNew = Model.dataMasterAfterNew;
		options.onBeforeDelete = Model.dataMasterBeforeDelete;
		options.onAfterDelete = Model.dataMasterAfterDelete;
		options.onValueChanged = Model.dataMasterValueChanged;
		options.onDataChanged = Model.dataMasterDataChanged;
		options.onBeforeSave = Model.dataMasterBeforeSave;
		options.onAfterSave = Model.dataMasterAfterSave;
		options.onNewCreateParam = Model.dataMasterNewCreateParam;
		
		Model.dataMaster = new BizData(options);
//		Model.objects.dataMaster = Model.dataMaster;
		//监视对象中的所有变动
		Model.dataMaster.deregRows = $scope.$watch('Model.dataMaster.rows',Model.dataMaster.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值

		
		var options = {};
		options.container = Model;
		options.concept = 'AccountPeriod';
		options.name = 'dataDetail';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
		options.creater = '/accounting/accountYear/single/createDetail';
		options.reader = '/accounting/accountYear/single/queryDetail';
		options.writer = '';
		options.deleter = '';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
		options.isTree = false;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.pathRelation = '';
		options.master = {"data":"dataMaster","relation":"fMasterID"};
		options.directDeleteMode = false;
		options.orderBy = 'periodNo';
		

		options.onIndexChanging = null;
		options.onIndexChanged = Model.dataDetailIndexChanged;
		options.onBeforeNew = Model.dataDetailBeforeNew;
		options.onAfterNew = Model.dataDetailAfterNew;
		options.onBeforeDelete = Model.dataDetailBeforeDelete;
		options.onAfterDelete = Model.dataDetailAfterDelete;
		options.onValueChanged = Model.dataDetailValueChanged;
		options.onDataChanged = Model.dataDetailDataChanged;
//		options.rowsWatcherFn = Model.rowsWatcherFn;
		
		Model.dataDetail = new BizData(options);
//		Model.objects.dataDetail = Model.dataDetail;
		//监视对象中的所有变动
		Model.dataDetail.deregRows = $scope.$watch('Model.dataDetail.rows',Model.dataDetail.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
		//////////////////////////////////////////////
		
		Model.dataMaster.refreshData();
		
		$scope.tabpanel = 'list';
		
	    return Model;
	}	
	
